def build_query(base_sql, params=None, for_count=False):
    """
    根据查询参数动态构建sql查询语句
    """
    conditions = []
    values = []
    values.append(params["user_id"])
    # 固定条件部分（始终添加）
    fixed_conditions = [
        "t.is_delete = FALSE and tu.user_id = %s",
    ]

    # 动态处理前端传入的参数，添加空值检查
    if params:
        if 'l_name' in params and params['l_name']:
            # 确保列表不为空
            placeholders = ','.join(['%s'])
            conditions.append(f"l.name IN ({placeholders})")
            values.append(params['l_name'])

        if 't_completed' in params and params['t_completed']:
            # 确保列表不为空
            placeholders = ','.join(['%s'] * len(params['t_completed']))
            conditions.append(f"t.completed IN ({placeholders})")
            values.extend(params['t_completed'])

        if 't_status' in params and params['t_status']:
            # 确保列表不为空
            placeholders = ','.join(['%s'] * len(params['t_status']))
            conditions.append(f"tu.status IN ({placeholders})")
            values.extend(params['t_status'])

    # 组合所有条件
    all_conditions = fixed_conditions
    if conditions:
        all_conditions.append("(" + " AND ".join(conditions) + ")")

    where_clause = ' AND '.join(all_conditions)

    # 构建完整的sql
    full_sql = f"{base_sql} WHERE {where_clause}"

    # 添加分页功能
    if not for_count and 'page' in params and 'rows' in params:
        page = int(params['page'])
        rows = int(params['rows'])
        # 计算偏移量
        offset = (page - 1) * rows
        full_sql += f"\n        LIMIT {rows} OFFSET {offset}"
        # 从参数中移除分页参数，避免作为条件参数
        params = {k: v for k, v in params.items() if k not in ['page', 'rows']}

    return full_sql, values

def teacher_build_query(base_sql, params=None, for_count=False):
    """
    根据查询参数动态构建sql查询语句
    """
    conditions = [
        "t.is_delete = FALSE",
    ]
    values = []

    fixed_conditions = []

    # 动态处理前端传入的参数，添加空值检查
    if params:
        if 'langName' in params and params['langName']:
            # 确保列表不为空
            placeholders = ','.join(['%s'])
            conditions.append(f"t.language_id IN ({placeholders})")
            values.append(params['langName'])

        if 'module' in params and params['module']:
            # 确保列表不为空
            placeholders = ','.join(['%s'])
            conditions.append(f"t.module_id IN ({placeholders})")
            values.append(params['module'])

        if 'leave' in params and params['leave']:
            # 确保列表不为空
            placeholders = ','.join(['%s'] * len(params['leave']))
            conditions.append(f"t.completed IN ({placeholders})")
            values.extend(params['leave'])

    # 组合所有条件
    all_conditions = fixed_conditions
    if conditions:
        all_conditions.append("(" + " AND ".join(conditions) + ")")

    where_clause = ' AND '.join(all_conditions)

    # 构建完整的sql
    full_sql = f"{base_sql} WHERE {where_clause}"

    # 添加分页功能
    if not for_count and 'page' in params and 'rows' in params:
        page = int(params['page'])
        rows = int(params['rows'])
        # 计算偏移量
        offset = (page - 1) * rows
        full_sql += f"\n        LIMIT {rows} OFFSET {offset}"
        # 从参数中移除分页参数，避免作为条件参数
        params = {k: v for k, v in params.items() if k not in ['page', 'rows']}

    return full_sql, values